import junit.framework.TestCase;
import me.zhenqu.model.bo.PascodeBo;
import me.zhenqu.model.bo.StatusBo;
import me.zhenqu.model.bo.TimeLineBo;
import me.zhenqu.model.bo.UserBo;
import me.zhenqu.model.pojo.entity.Pascode;
import me.zhenqu.model.pojo.entity.Status;
import me.zhenqu.model.pojo.entity.User;
import me.zhenqu.services.StatusService;
import me.zhenqu.services.UserService;
import me.zhenqu.services.cache.AbstractCache;
import me.zhenqu.services.cache.CacheMaker;
import java.util.Date;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: yibing.tan
 * Date: 11-11-11
 * Time: 上午11:09
 * To change this template use File | Settings | File Templates.
 */
public class ServiceTestCase extends TestCase {
    UserService us;
    StatusService ts;
    protected void setUp(){
        BoFactory boFactory = new BoFactory();
        us = new UserService();
        UserBo bo = (UserBo)boFactory.get("userBo");
        TimeLineBo timeLineBo = (TimeLineBo)boFactory.get("timeLineBo");
        StatusBo statusBo = (StatusBo)boFactory.get("statusBo");
        us.setBo(bo);
        us.setStatusBo(statusBo);
        us.setTimeLineBo(timeLineBo);
        us.setPascodeBo((PascodeBo)boFactory.get("pascodeBo"));
        CacheMaker cacheMaker = new CacheMaker();
        cacheMaker.setCache((AbstractCache) boFactory.get("cache"));
        us.setCacheMaker(cacheMaker);
        ts = new StatusService();
        ts.setBo(bo);
        ts.setTimeLineBo(timeLineBo);
        ts.setCacheMaker(cacheMaker);
        ts.setStatusBo(statusBo);
    }

    public void testUser(){
        User u = new User();
        u.setNick_name("ybtan");
        u.setGender(0);
        u.setCreate_time(System.currentTimeMillis());
        us.save(u);

        u = us.findByNickName("ybtan");
        u.setNick_name("ybtan1");
        us.update(u);

        us.delete(u);
        System.out.println("testUser done");
    }

    public void testSign(){
        User u = new User();
        u.setNick_name("ybtan");
        u.setGender(0);
        u.setCreate_time(System.currentTimeMillis());

        Pascode ps = new Pascode();
        ps.setPassword("123");
        us.createUser(u, ps);

        System.out.println(us.signIn("ybtan", "123"));

        us.delete(u);
        us.deletePascode(ps);

        System.out.println("testSign done");
    }

    public void testPublish(){
        Long uid = 2l;
        User user = us.getById(uid);
        if (user==null)
        {
            System.err.println("no user:"+uid+" exist!");
            return;
        }

        Status st = new Status();
        st.setUser_id(uid);
        st.setText("hello, zhenqu!");
        st.setPubtime(new Date());

        ts.publish(st);

        ts.delete(st.getStatus_id());

        System.out.println("testPublish done");
    }

    public void testSearch(){
        List<User> usd = us.searchUserIds("win");
        assert(usd.size()==1);
        System.out.println("testSearch done");
    }

}
